package com.songshuai.webapi.crm.controller;


import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.OssUploadUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import com.songshuai.webapi.common.upload.BaseUploadFile;
import com.songshuai.webapi.crm.dto.request.customerfollow.*;
import com.songshuai.webapi.crm.dto.response.customerfollow.BatchCustomerListResDto;
import com.songshuai.webapi.crm.dto.response.customerfollow.CustomArticleResDto;
import com.songshuai.webapi.crm.dto.response.customerfollow.ListRecordResDto;
import com.songshuai.webapi.crm.dto.response.customerfollow.MarkingFailureForCustomResDto;
import com.songshuai.webapi.crm.service.CustomerFollowService;
import com.songshuai.webapi.crm.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Slf4j
@Api("学员跟进")
@RestController
@RequestMapping("/api/customerfollow/")
public class CustomerFollowController extends BaseController{

    @Autowired
    private CustomerFollowService customerFollowService;
    @Autowired
    private SnowFlakeUtil snowFlakeUtil;
    @Autowired
    private OssUploadUtil uploadUtil;

    @Value("${file.basePath}")
    private String fileBasePath;




    @ApiOperation(value = "图片上传")
    @RequestMapping(value = "uploadPicture", method = RequestMethod.POST)
    public QueryResponse uploadPicture(@RequestParam("file") MultipartFile file) {

        try{
            BaseUploadFile baseUploadFile=new BaseUploadFile(snowFlakeUtil,fileBasePath,uploadUtil);
            Map<String, Object> map = baseUploadFile.uploadFile(file,"image");
            Integer code=Integer.parseInt(String.valueOf(map.get("code")));
            String msg=String.valueOf(map.get("msg"));
            map.remove("code");
            map.remove("msg");
            return QueryResponse.setResData(map,code,msg);

        } catch (Exception ex) {
            log.error("api/customerfollow/uploadPicture", ex);
            throw new BusinessException(ResultStatus.ERROR_PIC_UPLOAD.getErrorMsg(), ResultStatus.FAILOfficial);
        }
    }

    /**
     * 编辑学员详情
     */
    @ApiOperation(value = "编辑学员详情")
    @RequestMapping(value = "editCustomArticle", method = RequestMethod.POST)
    public QueryResponse editCustomArticle(@RequestBody EditCustomArticleReqDto request) {

        try{
            customerFollowService.editCustomArticle(request);
            return QueryResponse.setResData(null);
        } catch (Exception e) {
            log.error("api/customerfollow/editCustomArticle", e);
            e.printStackTrace();
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }



    /**
     * 学员详情
     */
    @ApiOperation(value = "学员详情")
    @RequestMapping(value = "customArticle", method = RequestMethod.POST)
    public QueryResponse customArticle(@RequestBody CustomArticleReqDto request) {

        try{
            CustomArticleResDto CustomArticleResDto = customerFollowService.customArticle(request);
            return QueryResponse.setResData(CustomArticleResDto);
        } catch (Exception e) {
            log.error("api/customerfollow/customArticle", e);
            e.printStackTrace();
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }

    /**
     * 标记学员失效
     */

    @ApiOperation(value = "标记学员失效")
    @RequestMapping(value = "markingFailureForCustom", method = RequestMethod.POST)
    public QueryResponse markingFailureForCustom(@RequestBody MarkingFailureForCustomReqDto request) {

        try{

            MarkingFailureForCustomResDto markingFailureForCustomResDto = customerFollowService.markingFailureForCustom(request);
            return QueryResponse.setResData(markingFailureForCustomResDto);
        } catch (Exception e) {
            log.error("api/customerfollow/markingFailureForCustom", e);
            e.printStackTrace();
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }


    /**
     * 学员删除
     */
    @ApiOperation(value = "学员批量删除")
    @RequestMapping(value = "deleteCustom", method = RequestMethod.POST)
    public QueryResponse deleteCustom(@RequestBody DeleteCustomReqDto request) {

        try{
            customerFollowService.deleteCustom(request);
            return QueryResponse.setResData(null);
        } catch (Exception e) {
            log.error("api/customerfollow/deleteCustom", e);
            e.printStackTrace();
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }


    /**
     * 批量分配销售（流转记录）
     */
    @ApiOperation(value = "批量分配销售-流转记录")
    @RequestMapping(value = "batchAssign", method = RequestMethod.POST)
    public QueryResponse batchAssign(@RequestBody BatchAssignReqDto request) {

        try{

            customerFollowService.batchAssign(request);
            return QueryResponse.setResData(null);

        } catch (Exception e) {
            log.error("api/customerfollow/batchAssign", e);
            e.printStackTrace();
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }

    /**
     * 批量分配-人员列表
     */
    @ApiOperation(value = "批量分配-人员列表")
    @RequestMapping(value = "batchUserList", method = RequestMethod.POST)
    public QueryResponse batchUserList(@RequestBody BatchUserListReqDto request) {


        try{
            List<Map> lists = customerFollowService.batchUserList(request);
            return QueryResponse.setResData(lists);

        } catch (Exception e) {
            log.error("api/customerfollow/batchUserList", e);
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }

    /**
     * 批量分配-学员列表****
     */
    @ApiOperation(value = "批量分配-学员列表")
    @RequestMapping(value = "batchCustomerList", method = RequestMethod.POST)
    public QueryResponse<List<BatchCustomerListResDto>> batchCustomerList(@RequestBody BatchCustomerListReqDto request) {

        try{
            List<BatchCustomerListResDto> list = customerFollowService.batchCustomerList(request);
            return QueryResponse.setResData(list);

        } catch (Exception ex) {
            log.error("api/customerfollow/batchCustomerList", ex);
            throw new BusinessException(ex.getMessage(), ResultStatus.FAILOfficial);
        }
    }



    /**
     * 学员跟进列表*****
     */
    @ApiOperation(value = "学员跟进列表")
    @RequestMapping(value = "listRecord", method = RequestMethod.POST)
    public QueryResponse<List<ListRecordResDto>> listRecord(@RequestBody BaseParmDto<ListRecordReqDto> request) {

        try{

            PageUtil page = customerFollowService.listRecord(request);
            List<ListRecordResDto> listRecordResDtos = (List<ListRecordResDto>)page.getLists();
            return QueryPageRes.setQueryResponseData(listRecordResDtos,page.getTotalSize()==null?0L:page.getTotalSize().longValue());

        } catch (Exception ex) {
            log.error("api/customerfollow/listRecord", ex);
            throw new BusinessException(ex.getMessage(), ResultStatus.FAILOfficial);
        }
    }

    /**
     * 添加学员跟进记录****
     */
    @ApiOperation(value = "添加学员跟进记录")
    @RequestMapping(value = "addRecord", method = RequestMethod.POST)
    public QueryResponse addRecord(@RequestBody AddRecordReqDto request) {
        try{

            customerFollowService.addRecord(request);
            return QueryResponse.setResData(null);
        } catch (Exception e) {
            log.error("api/customerfollow/addRecord", e);
            e.printStackTrace();
            throw new BusinessException(e.getMessage(), ResultStatus.FAILOfficial);
        }
    }


}
